package com.example.moblie203zlw_databases11_05.slice;
import com.example.moblie203zlw_databases11_05.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class MainAbilitySlice extends AbilitySlice {
    Image image_add,image_menu;
    DatabaseHelper databaseHelper;
    DataAbilityHelper dataAbilityHelper;

    public static RdbStore getRdbStore() {
        return rdbStore;
    }

   public static RdbStore rdbStore;
    ListContainer listcontainer;
    List <itme> list;
    Text text;
    itme itme;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        databaseHelper=new DatabaseHelper(this);
        dataAbilityHelper=DataAbilityHelper.creator(this);
        image_add= (Image) findComponentById(ResourceTable.Id_image_add);
        image_menu= (Image) findComponentById(ResourceTable.Id_image_menu);
        listcontainer= (ListContainer) findComponentById(ResourceTable.Id_listcontainer);
        text = (Text) findComponentById(ResourceTable.Id_text_visi);
        StoreConfig storeConfig=StoreConfig.newDefaultConfig("student.db");
        RdbOpenCallback rdbOpenCallback=new RdbOpenCallback() {
            @Override
            public void onCreate(RdbStore rdbStore) {
                rdbStore.executeSql("create table mobile(name varchar(10) " +
                        "not null ,sex varchar(3) default '保密' not null,phone int not null)");
            }
            @Override
            public void onUpgrade(RdbStore rdbStore, int i, int i1) {
            }
        };
        rdbStore=databaseHelper.getRdbStore(storeConfig,1,rdbOpenCallback);
        image_add.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                CommonDialog dialog=new CommonDialog(MainAbilitySlice.this);
                Component component_dialog= LayoutScatter.getInstance(MainAbilitySlice.this)
                        .parse(ResourceTable.Layout_dialog_add_contacts,null,true);
                Button btnConfirm = (Button)
                        component_dialog.findComponentById(ResourceTable. Id_btn_confirm );
                Button btnCacel = (Button)
                        component_dialog.findComponentById(ResourceTable. Id_btn_cancel );
                TextField  nameTf = (TextField)
                        component_dialog.findComponentById(ResourceTable. Id_tf_name );
                TextField  phoneTf = (TextField)
                        component_dialog.findComponentById(ResourceTable. Id_tf_phone );
                RadioContainer  genderRc = (RadioContainer)
                        component_dialog.findComponentById(ResourceTable. Id_rc_gender );
                RadioButton  manRb = (RadioButton)
                        component_dialog.findComponentById(ResourceTable. Id_rb_man );
                RadioButton  ladyRb = (RadioButton)
                        component_dialog.findComponentById(ResourceTable. Id_rb_lady );
                manRb.setChecked(true);
                btnConfirm.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        list=new ArrayList<>();
                        String sexTfText=null;
                        if (manRb.isChecked()){
                             sexTfText="男";
                        }
                        if (ladyRb.isChecked()){
                             sexTfText="女";
                        }
                        String nameTfText = nameTf.getText();
                        String phoneTfText = phoneTf.getText();
                        if (nameTf!=null && phoneTf!=null){
                            ValuesBucket valuesBucket=new ValuesBucket();
                            valuesBucket.putString("name",nameTfText);
                            valuesBucket.putString("sex",sexTfText);
                            valuesBucket.putString("phone",phoneTfText);
                            long mobile = rdbStore.insert("mobile", valuesBucket);
                            String s[]={"name","sex","phone"};
                            RdbPredicates rdbPredicates=new RdbPredicates("mobile");
                            ResultSet resultSet = rdbStore.query(rdbPredicates, s);
                            if (resultSet.goToFirstRow()){
                                do {
                                    String name=resultSet.getString(0);
                                    int phone=resultSet.getInt(2);
                                    String sex=resultSet.getString(1);
                                    itme=new itme(name,phone,sex);
                                    list.add(itme);
                                }while (resultSet.goToNextRow());
                                        if (list.size()>0){
                                             text.setVisibility(Component.HIDE);
                                        }
                            }
                            item_provider item_provider=new item_provider(list,MainAbilitySlice.this);
                            listcontainer.setItemProvider(item_provider);
                            new ToastDialog(MainAbilitySlice.this).setText("输入成功").show();
                        }else {
                            new ToastDialog(MainAbilitySlice.this).setText("输入错误").show();
                        }
                            dialog.remove();
                    }
                });
                btnCacel.setClickedListener(new Component.ClickedListener() {
                    @Override
                    public void onClick(Component component) {
                        new ToastDialog(MainAbilitySlice.this).setText("您取消了").show();
                        dialog.remove();
                    }
                });
                dialog.setContentCustomComponent(component_dialog);
                dialog.show();
            }
        });
    }

    private void qruite() {
        String s[]={"name","sex","phone"};
        RdbPredicates rdbPredicates=new RdbPredicates("mobile");
        ResultSet resultSet = rdbStore.query(rdbPredicates, s);
        if (resultSet.goToFirstRow()){
        do {
            String name=resultSet.getString(0);
            int phone=resultSet.getInt(2);
            String sex=resultSet.getString(1);
            itme=new itme(name,phone,sex);
            list.add(itme);
        }while (resultSet.goToNextRow());
        if (list.size()>0){
            text.setVisibility(Component.HIDE);
            item_provider item_provider=new item_provider(list,MainAbilitySlice.this);
            listcontainer.setItemProvider(item_provider);
        }else {
            System.out.println(""+list.size());
        }
    }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
